package com.earthea.scaffold.common.entity;

import cn.hutool.core.text.NamingCase;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.earthea.scaffold.common.util.SecurityUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@Getter
@Setter
@ToString
public class BaseReq<T> extends Page<T> {
    /**
     * 页码
     */
    private Integer pageNum;

    /**
     * 每页大小
     */
    private Integer pageSize;

    /**
     * 开始时间
     */
    private String beginTime;

    /**
     * 结束时间
     */
    private String endTime;

    /**
     * 操作人
     */
    private Long userId = SecurityUtils.getLoginUserId();

    /**
     * 排序字段
     */
    private String orderByColumn;
    /**
     * ascending升序，descending降序
     */
    private String isAsc;

    public long getCurrent() {
        return pageNum == null ? 1L : pageNum;
    }

    public long getSize() {
        return pageSize == null ? 10L : pageSize;
    }

    public BaseReq<T> addOrder() {
        if (StringUtils.isNotBlank(orderByColumn)) {
            OrderItem orderItem = new OrderItem();
            orderItem.setColumn(NamingCase.toUnderlineCase(orderByColumn));
            orderItem.setAsc("ascending".equalsIgnoreCase(isAsc));
            addOrder(orderItem);
        }
        return this;
    }
}
